﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ListItemEditor.UI
{
    public partial class ModifyViewForm : Form
    {
        public ModifyViewForm()
        {
            InitializeComponent();
        }

        public List<string> AvailableViewFields
        {
            get
            {
                return msFields.Choices == null ? new List<string>() : (List<string>)msFields.Choices;
            }
            set
            {
                msFields.Choices = value;
            }
        }

        public List<string> SelectedViewFields
        {
            get
            {
                return msFields.SelectedValues == null ? new List<string>() : msFields.SelectedValues.Cast<string>().ToList();
            }
            set
            {
                msFields.SelectedValues = value.Cast<object>().ToList();
            }
        }

        public string ViewTitle
        {
            get
            {
                return this.txtTitle.Text;
            }
            set
            {
                this.txtTitle.Text = value;
            }
        }

        public string ViewQuery
        {
            get
            {
                return this.txtQuery.Text;
            }
            set
            {
                this.txtQuery.Text = value;
            }
        }

        public bool ViewIsDefault
        {
            get
            {
                return this.cbDefault.Checked;
            }
            set
            {
                this.cbDefault.Checked = value;
            }
        }

        private void ModifyViewForm_Load(object sender, EventArgs e)
        {
            this.msFields.DataBind();
        }

        public static DialogResult ShowModifyViewForm(IWin32Window parent, string caption, bool isNew, ref ViewData viewData, List<string> availableFields)
        {
            ModifyViewForm form = new ModifyViewForm() 
            {
                ViewIsDefault = viewData.Default,
                ViewQuery = viewData.Query,
                ViewTitle = viewData.Name,
                AvailableViewFields = availableFields,
                SelectedViewFields = viewData.ViewFields,
                Text = caption
            };

            if (!isNew) form.txtTitle.Enabled = false;
            else form.txtTitle.Text = string.Empty;

            DialogResult result = form.ShowDialog(parent);
            if (result == DialogResult.OK)
            {
                viewData = new ViewData()
                {
                    Name = form.ViewTitle,
                    Default = form.ViewIsDefault,
                    Query = form.ViewQuery,
                    ViewFields = form.SelectedViewFields
                };
            }
            return result;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtTitle.Text))
            {
                MessageBox.Show(this, "Title cannot be empty.");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}